package com.atguigu.common.valid;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.HashSet;
import java.util.Set;

/**
 *自定义注解校验器
 * @author: jialiangming
 * @date: Created in 2020/5/22 17:42
 * @version: V1.0
 * @copyright: Copyright(c) chinaunicom， All rights reserved.
 */
public class ListValueConstraintValidator implements ConstraintValidator<ListValue,Integer> {
    private Set<Integer> set =new HashSet<>();
    //初始化方法
    @Override
    public void initialize(ListValue constraintAnnotation) {
        int[] valus = constraintAnnotation.valus();
        for(int val:valus){
            set.add(val);
        }
    }
    //判断校验是否成功
    /**
     * * @param value需要校验的值
     * @return
     * @throws
     */
    @Override
    public boolean isValid(Integer value, ConstraintValidatorContext context) {
        return set.contains(value);
    }
}
